package com.visikard.asynctask;

import android.os.Handler;
import android.os.Message;

import com.visikard.application.UIMessage;
import com.visikard.business.CouponBUS;
import com.visikard.item.ItemCouponsBuilderCreate;
import com.visikard.response.BaseResponse;
import com.visikard.response.CreateCouponResponse;

public class CreateCouponTask extends BaseTask {

	public static final int POS_UID = 0;
	public static final int POS_USERID = 1;
	public static final int POS_ITEM = 2;

	public CreateCouponTask(Handler handler) {
		super(handler);
	}

	public CreateCouponTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doBackgroundWork(Object... params) {

		long uid = (Long) params[POS_UID];
		String userId = (String) params[POS_USERID];
		ItemCouponsBuilderCreate item = (ItemCouponsBuilderCreate) params[POS_ITEM];

		CouponBUS busObj = new CouponBUS();
		return busObj.createCoupon(uid, userId, item);
	}

	@Override
	protected boolean handleResult(Object result) {
		CreateCouponResponse response = (CreateCouponResponse) result;
		if (response != null) {
			switch (response.getErrorCode()) {
				case BaseResponse.SUCCESS:
					Message successMsg = new Message();
					successMsg.what = UIMessage.MSG_CREATE_UPDATE_COUPON_FINISH;
					if (!response.getMessage().isEmpty()) {
						successMsg.arg1 = 1;
					}
					else {
						successMsg.arg1 = 0;
					}
					mUIHandler.sendMessage(successMsg);
					return true;
			}
		}
		return false;
	}

}
